import router from "./index.js";
import store from '../store/index.js';
import { Message } from 'element-ui';

const whiteList=['/login'];
console.log(whiteList);

const typeList = ["/account/add", "/account/list"];
/*
beforeEach((to,from,next)=>{} )
*/

router.beforeEach((to, from, next)=>{
    console.log("路由守卫运行了", to, from, next);
    if (store.getters.token){
        if (to.path=='/login'){
            next("/home/dashboard");
        }else if (typeList.indexOf(to.path)!=-1){
            if (store.getters.type=="1"){
                next();
            }else {
                //非超级管理员
                console.log("退出登录");
                Message.error("您没有权限访问改页面,请使用超级管理员账号重新登录");
                //发送退出动作
                store.dispatch("LogoutAction");
                next("/login")
            }
        }else {
            next();
        }
    }else {
        if (whiteList.indexOf(to.path)!=-1){
            next();
        }else {
            next("/login");
        }
    }
})